﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace llAma
{
    //llAma public utilities and helper functions go here.
    //This includes the timestamp code, so it can be accessable
    //Anywhere.
    public static class Utility
    {
        static MD5 MD5Hasher = MD5.Create();
        static SHA1 SHAHasher = SHA1.Create();
        private static byte[] ComputeSHA1(byte[] newData)
        {
            return SHAHasher.ComputeHash(newData);
        }
        private static byte[] ComputeMD5(byte[] newData, int offset, int length)
        {
            return MD5Hasher.ComputeHash(newData, offset, length);
        }
        private static byte[] ComputeMD5(byte[] newData)
        {
            return MD5Hasher.ComputeHash(newData);
        }
        private static string md5(byte[] data, int offset, int length)
        {
            string ret = BitConverter.ToString(ComputeMD5(data, offset, length)).Replace("-", "").ToLower();
            return ret;
        }
        /// <summary>
        /// Create an SHA1 hash as a string from a string.
        /// </summary>
        /// <param name="data">the string (UTF8) to be hashed</param>
        /// <returns>An SHA1 hash of of the string supplied</returns>
        public static string sha1(string data)
        {
            Encoding UTF8 = UTF8Encoding.UTF8;
            string ret = BitConverter.ToString(ComputeSHA1(UTF8.GetBytes(data))).Replace("-", "").ToLower();
            return ret;
        }
        /// <summary>
        /// Creates an MD5 hash of a string as a string.
        /// </summary>
        /// <param name="data">String to be hashed (UTF8)</param>
        /// <returns>MD5 of the string passed.</returns>
        public static string md5(string data)
        {
            Encoding UTF8 = UTF8Encoding.UTF8;
            string ret = BitConverter.ToString(ComputeMD5(UTF8.GetBytes(data))).Replace("-", "").ToLower();
            return ret;
        }
    }
}
